package org.jsbd.boss.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.dao.IChannelApkInfoDAO;
import org.jsbd.boss.dao.IDmChannelRetentDAO;
import org.jsbd.boss.domian.ChannelApkInfo;
import org.jsbd.boss.domian.ChannelMonitorInfo;
import org.jsbd.boss.domian.ChannelRetentLogInfo;
import org.jsbd.boss.domian.ChannelRetentMonitorInfo;
import org.jsbd.boss.domian.IncomeInfo;
import org.jsbd.boss.service.IIncomeService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 动漫渠道刷任务
 * 
 * @author <a href="mailto:zhangbinghua@zhexinit.com" >zbh</a>
 * @version 1.0.0
 */
@Service
public class ChannelRetentLogService {
	public static final Logger LOGGER = LoggerFactory.getLogger(ChannelRetentLogService.class);

	@Autowired
	private RedisClient redisClient;

	@Autowired
	private IDmChannelRetentDAO dmChannelRetentDAO;
	
    @Autowired
    private IChannelApkInfoDAO channelApkInfoDAO;
	
	@Autowired
	private IIncomeService incomeService;

	public List<ChannelRetentLogInfo> getChannelRetentALLLog(ChannelRetentLogInfo channelRetentLogInfo) {
		return dmChannelRetentDAO.getChannelRetentALLLog(channelRetentLogInfo);
	}
	public List<ChannelRetentLogInfo> getChannelRetentNewUserLog(ChannelRetentLogInfo channelRetentLogInfo) {
		List<ChannelRetentLogInfo>  channelRetentLogInfolist = null;//dmChannelRetentDAO.getChannelRetentNewUserLog(channelRetentLogInfo);
		List<ChannelRetentLogInfo>  resultlist = new ArrayList<ChannelRetentLogInfo>();
//		if (channelRetentLogInfolist != null && channelRetentLogInfolist.size() > 0) {
//			for (ChannelRetentLogInfo temp : channelRetentLogInfolist) {
//				IncomeInfo incomeInfo =new IncomeInfo();
//				incomeInfo.setDate(temp.getGenertime());
//				incomeInfo.setMark(temp.getMark());
//				IncomeInfo rincomeInfo =incomeService.getIncomeInfo(incomeInfo);
//				if (rincomeInfo != null) {
//					temp.setIncome(rincomeInfo.getIncome());
//				}
//				resultlist.add(temp);
//			}
//		}
		return resultlist;
	}
	
	public List<ChannelRetentLogInfo> getChannelRetentLog(ChannelRetentLogInfo channelRetentLogInfo) {
		return dmChannelRetentDAO.getChannelRetentLog(channelRetentLogInfo);
	}
	
	public List<ChannelMonitorInfo> getChannelMonitorInfo(String time) {
		
		List<ChannelMonitorInfo> resultlist  =  dmChannelRetentDAO.getChannelMonitorInfo(time);
		for(ChannelMonitorInfo temp : resultlist){
			ChannelApkInfo  t = channelApkInfoDAO.getConfigByMarkForPage(temp.getMark());
			if (t != null) {
				temp.setApkName(t.getApkName());
			}
		}
		return resultlist ;
	}
	
	public List<ChannelRetentMonitorInfo> getChannelRetentMonitorInfo(String time) {
		List<ChannelRetentMonitorInfo> resultlist  = dmChannelRetentDAO.getChannelRetentMonitorInfo(time);
		for(ChannelRetentMonitorInfo temp : resultlist){
			ChannelApkInfo  t = channelApkInfoDAO.getConfigByMarkForPage(temp.getMark());
			if (t == null) {
				continue;
			}
			temp.setApkName(t.getApkName());
			String retentdate=t.getRetentDates();
			int imsiRetentrate = Integer.parseInt(t.getRetentRate());
			int imsiCurrentRetentrate = Integer.parseInt(t.getCurrRetentRate());
			
			temp.setRetentdate(retentdate);
			temp.setRatio(imsiRetentrate+"");
			temp.setIsretentlimit(t.getIsretentlimit());
			
			// 根据配置
			String[] dates = retentdate.split(",");
				for (int i = 0; i < dates.length; i++) {
					
					int predate = Integer.parseInt(dates[i]);
					SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
					Calendar cal = Calendar.getInstance();
					cal.add(Calendar.DATE, -predate);
					String date = df.format(cal.getTime());
					int retentnum = 0;
					//如果是当天，则取当日留存比例
					if("0".equals(predate)){
						 retentnum = (int) (dmChannelRetentDAO.getChannelPlanRetentInfo( temp.getMark(),date)*(imsiCurrentRetentrate / 100.0));
					}else{
					//需要执行的任务数量
						retentnum = (int) (dmChannelRetentDAO.getChannelPlanRetentInfo( temp.getMark(),date)*(imsiRetentrate / 100.0));
					}
					temp.setPlanNum(temp.getPlanNum()+retentnum);
					
			}
			
		}
		return resultlist ;
	}
}
